package com.sanjiahantang.ythshopping.activity.me;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.sanjiahantang.ythshopping.R;
import com.sanjiahantang.ythshopping.activity.BaseActivity;
import com.sanjiahantang.ythshopping.model.Area;
import com.sanjiahantang.ythshopping.model.DillBean;
import com.sanjiahantang.ythshopping.model.DillModel;
import com.sanjiahantang.ythshopping.presenter.DillPresenter;
import com.sanjiahantang.ythshopping.utils.DillWindow;
import com.sanjiahantang.ythshopping.views.DillView;

import java.util.ArrayList;

/**
 * Created by admin on 2017/7/25.
 */

public class DillDetailActivity extends BaseActivity implements DillView, View.OnClickListener {

    private CheckBox cb_need;
    private LinearLayout ll_all;
    public DillPresenter dillPresenter;
    public TextView tv_type, tv_main, tv_city;
    public DillModel dillmodel;
    public LinearLayout ll_unitype;
    public ScrollView sv_comtype;
    public CheckBox cb_personal, cb_company;
    public static final int AREA = 2;
    private EditText et_dilltop, et_comname, et_ppnum, et_comaddress, et_comphone, et_bank, et_banknum, et_name, et_phone, et_address;
    private Button btn_submit;
    private LinearLayout ll_city;
    private DillBean bean;
    private boolean open = false;
    private TextView title;
    private FrameLayout back;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public void initData() {
        super.initData();
        dillPresenter = new DillPresenter(mContext, this);
        dillPresenter.LoadDillMsg();
    }

    @Override
    public void initTitle() {
        title = (TextView) findViewById(R.id.title);
        title.setText("发票详情");
        back = (FrameLayout) findViewById(R.id.fl_Left);
        back.setOnClickListener(this);
    }

    @Override
    public void initView() {
        super.initView();
        setContentView(R.layout.activity_dell);
        bean = new DillBean();
        cb_need = (CheckBox) findViewById(R.id.cb_dill);
        ll_all = (LinearLayout) findViewById(R.id.ll_dill_all);
        tv_type = (TextView) findViewById(R.id.tv_dill_type);
        tv_main = (TextView) findViewById(R.id.tv_dill_main);
        ll_unitype = (LinearLayout) findViewById(R.id.ll_dill_unitype);
        sv_comtype = (ScrollView) findViewById(R.id.sv_dill_uptype);
        cb_company = (CheckBox) findViewById(R.id.cb_dill_company);
        cb_personal = (CheckBox) findViewById(R.id.cb_dill_personal);
        et_dilltop = (EditText) findViewById(R.id.et_dill_top);
        btn_submit = (Button) findViewById(R.id.btn_dill_submit);
        ll_city = (LinearLayout) findViewById(R.id.ll_dill_city);
        tv_city = (TextView) findViewById(R.id.tv_dill_city);
        et_address = (EditText) findViewById(R.id.et_dill_useraddress);
        et_bank = (EditText) findViewById(R.id.et_dill_bank);
        et_banknum = (EditText) findViewById(R.id.et_dill_banknum);
        et_comaddress = (EditText) findViewById(R.id.et_dill_comaddress);
        et_comname = (EditText) findViewById(R.id.et_dill_comname);
        et_comphone = (EditText) findViewById(R.id.et_dill_comphone);
        et_name = (EditText) findViewById(R.id.et_dill_username);
        et_phone = (EditText) findViewById(R.id.et_dill_userphone);
        et_ppnum = (EditText) findViewById(R.id.et_dill_ppnum);
        ll_city.setOnClickListener(this);
        btn_submit.setOnClickListener(this);
        tv_main.setOnClickListener(this);
        tv_type.setOnClickListener(this);

        cb_need.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (b) {
                    ll_all.setVisibility(View.VISIBLE);
                    open = true;
                } else {
                    ll_all.setVisibility(View.GONE);
                    open = false;
                }
            }
        });
        cb_personal.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (compoundButton.isChecked()) {
                    if (b) {
                        cb_company.setChecked(false);
                        et_dilltop.setVisibility(View.GONE);
                        cb_company.setEnabled(true);
                        cb_personal.setEnabled(false);
                        bean.setInv_payee_type("individual_inv");
                    }
                }
            }
        });
        cb_company.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (compoundButton.isChecked()) {
                    if (b) {
                        cb_personal.setChecked(false);
                        et_dilltop.setVisibility(View.VISIBLE);
                        cb_company.setEnabled(false);
                        cb_personal.setEnabled(true);
                        bean.setInv_payee_type("unit_inv");
                    }
                }
            }
        });
    }

    @Override
    public void returnDill(DillModel model) {
        dillmodel = model;

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_dill_type:
                DillWindow typewindow = new DillWindow(DillDetailActivity.this, dillmodel.getData().getInvoice_type(), tv_type, new DillWindow.DillPopCallBack() {
                    @Override
                    public void callBack(String title, int position) {
                        tv_type.setText(title);
                        if (position == 0) {
                            ll_unitype.setVisibility(View.VISIBLE);
                            sv_comtype.setVisibility(View.GONE);

                        } else {
                            ll_unitype.setVisibility(View.GONE);
                            sv_comtype.setVisibility(View.VISIBLE);
                        }
                        bean.setInv_type(dillmodel.getData().getInvoice_type().get(position).getValue());
                        bean.setRate(dillmodel.getData().getInvoice_type().get(position).getRate());
                    }
                });
                typewindow.showPopupWindow(tv_type);
                break;
            case R.id.tv_dill_main:
                DillWindow mainwindow = new DillWindow(DillDetailActivity.this, dillmodel.getData().getInvoice_type(), dillmodel.getData().getInvoice_content(), tv_type, new DillWindow.DillPopCallBack() {
                    @Override
                    public void callBack(String title, int position) {
                        tv_main.setText(title);
                        bean.setInv_content(title);
                    }
                });
                mainwindow.showPopupWindow(tv_main);
                break;
            case R.id.btn_dill_submit:
                if (open) {
                    if (bean.getInv_content() == null) {
                        showToast("请选择发票内容");
                        return;
                    }

                    if (bean.getInv_type().equals("normal_invoice")) {
                        if (bean.getInv_payee_type() == null) {
                            showToast("请选择发票类型");
                            return;

                        } else if (bean.getInv_payee_type().equals("unit_inv")) {
                            if (et_dilltop.getText().equals("")) {
                                showToast("请填写发票抬头及税号");
                                return;
                            }
                            bean.setInv_payee(et_dilltop.getText().toString());
                        }

                    } else {
                        if (et_ppnum.getText().equals("") || et_phone.getText().equals("") || et_comname.getText().equals("") || et_name.getText().equals("") || et_comphone.getText().equals("") || et_comaddress.getText().equals("") || et_banknum.getText().equals("") || et_bank.getText().equals("") || et_address.getText().equals("") || bean.getInv_consignee_province() == null) {
                            showToast("请填写必要信息");
                            return;
                        } else {
                            bean.setVat_inv_company_name(et_comname.getText().toString());
                            bean.setVat_inv_taxpayer_id(et_ppnum.getText().toString());
                            bean.setVat_inv_registration_address(et_comaddress.getText().toString());
                            bean.setVat_inv_registration_phone(et_comphone.getText().toString());
                            bean.setVat_inv_deposit_bank(et_bank.getText().toString());
                            bean.setVat_inv_bank_account(et_banknum.getText().toString());
                            bean.setInv_consignee_name(et_name.getText().toString());
                            bean.setInv_consignee_phone(et_phone.getText().toString());
                            bean.setInv_consignee_address(et_address.getText().toString());
                        }

                    }
                    Intent intent = new Intent();
                    Bundle bundle = new Bundle();
                    bundle.putSerializable("dillbean", bean);
                    intent.putExtras(bundle);
                    intent.putExtra("open", "1");
                    setResult(2, intent);

                    finish();
                } else {
                    Intent intent = new Intent();
                    intent.putExtra("open", "0");
                    setResult(2, intent);
                    finish();
                }


                break;
            case R.id.ll_dill_city:
                Intent it = new Intent(mContext, SelectAreaActivity.class);
                startActivityForResult(it, 1);
                break;

            case R.id.fl_Left:
                finish();
                break;

        }

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (resultCode != RESULT_OK) {
            switch (resultCode) {
                case AREA:
                    String area = "";
                    Bundle b = data.getExtras();
                    ArrayList<Area> list = new ArrayList<>();
                    list = (ArrayList<Area>) b.getSerializable("arealist");
                    for (Area model : list) {
                        area = area + model.getRegion_name();
                    }
                    tv_city.setText("收票人省份：" + area);
                    bean.setInv_consignee_province(list.get(0).getRegion_id());
                    bean.setInv_consignee_city(list.get(1).getRegion_id());
                    bean.setInv_consignee_district(list.get(2).getRegion_id());
                    break;
            }
        }
    }
}
